package xmlcurl

import (
	"callsips/internal/model/account/asips"
	"callsips/internal/model/sips/cserver"
	"callsips/internal/model/sips/gateway"
	"callsips/internal/model/yunhu/mobile"
	"encoding/xml"
)

// 增加测试用户
func XMLDomainDirectoryTextUser() []*Domain {
	var domains []*Domain = make([]*Domain, 0)
	var paramDomain = make([]*Param, 0)
	paramDomain = append(paramDomain, &Param{XMLName: xml.Name{}, Attrname: "dial-string", Attrvalue: "{presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"})
	var userInfo = &User{
		XMLName: xml.Name{},
		Attrid:  "19238391110",
		Params: &Params{XMLName: xml.Name{}, Param: []*Param{
			&Param{XMLName: xml.Name{}, Attrname: "password", Attrvalue: "19238391110"},
		}},
		Variables: &Variables{XMLName: xml.Name{}, Variable: []*Variable{
			&Variable{XMLName: xml.Name{}, Attrname: "toll_allow", Attrvalue: "local"},
			&Variable{XMLName: xml.Name{}, Attrname: "accountcode", Attrvalue: "0-0"},
			&Variable{XMLName: xml.Name{}, Attrname: "user_context", Attrvalue: "default"},
			&Variable{XMLName: xml.Name{}, Attrname: "outbound_caller_id_name", Attrvalue: "$${outbound_caller_name}"},
			&Variable{XMLName: xml.Name{}, Attrname: "outbound_caller_id_number", Attrvalue: "$${outbound_caller_id}"},
		}},
		Gateways: &Gateways{XMLName: xml.Name{}},
	}

	var domain = Domain{
		XMLName:  xml.Name{},
		Attrname: "10.0.4.12",
		Params:   &Params{XMLName: xml.Name{}, Param: paramDomain},
		Groups: &Groups{
			XMLName: xml.Name{},
			Group: []*Group{
				&Group{
					XMLName:  xml.Name{},
					Attrname: "default",
					Users: &Users{
						XMLName: xml.Name{},
						User:    []*User{userInfo},
					},
				},
			},
		},
	}

	domains = append(domains, &domain)

	return domains
}

// 用户信息
func XMLDomainDirectoryUser(asipInfo *asips.AccountAsips, phoneInfo *mobile.YunhuMobile, cserverInfo *cserver.SipsCserver) []*Domain {
	var domains []*Domain = make([]*Domain, 0)
	var paramDomain = make([]*Param, 0)
	paramDomain = append(paramDomain, &Param{XMLName: xml.Name{}, Attrname: "dial-string", Attrvalue: "{presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"})

	var userInfo = &User{
		XMLName: xml.Name{},
		Attrid:  asipInfo.Username,
		Params: &Params{XMLName: xml.Name{}, Param: []*Param{
			&Param{XMLName: xml.Name{}, Attrname: "password", Attrvalue: asipInfo.Password},
			&Param{XMLName: xml.Name{}, Attrname: "sip-profile", Attrvalue: "external"},
		}},
		Variables: &Variables{XMLName: xml.Name{}, Variable: []*Variable{
			&Variable{XMLName: xml.Name{}, Attrname: "toll_allow", Attrvalue: asipInfo.TollAllow},
			&Variable{XMLName: xml.Name{}, Attrname: "accountcode", Attrvalue: asipInfo.Accountcode},
			&Variable{XMLName: xml.Name{}, Attrname: "user_context", Attrvalue: "public"},
			&Variable{XMLName: xml.Name{}, Attrname: "effective_caller_id_name", Attrvalue: phoneInfo.Phone},
			&Variable{XMLName: xml.Name{}, Attrname: "effective_caller_id_number", Attrvalue: phoneInfo.Phone},
			&Variable{XMLName: xml.Name{}, Attrname: "outbound_caller_id_name", Attrvalue: "$${outbound_caller_name}"},
			&Variable{XMLName: xml.Name{}, Attrname: "outbound_caller_id_number", Attrvalue: "$${outbound_caller_id}"},
		}},
		Gateways: &Gateways{XMLName: xml.Name{}},
	}

	var domain = Domain{
		XMLName:  xml.Name{},
		Attrname: cserverInfo.Domain,
		Params:   &Params{XMLName: xml.Name{}, Param: paramDomain},
		Groups: &Groups{
			XMLName: xml.Name{},
			Group: []*Group{
				&Group{
					XMLName:  xml.Name{},
					Attrname: "default",
					Users: &Users{
						XMLName: xml.Name{},
						User:    []*User{userInfo},
					},
				},
			},
		},
	}

	domains = append(domains, &domain)

	return domains
}

// ac控制
func XMLDomainNetworkListAcl() []*Configuration {

	var listNode []*List = []*List{
		&List{XMLName: xml.Name{}, Attrname: "domains", Attrdefault: "deny", Node: []*Node{
			&Node{XMLName: xml.Name{}, Attrtype: "allow", Attrdomain: "$${domain}"},
		}},
	}

	return []*Configuration{&Configuration{
		XMLName:         xml.Name{},
		Attrname:        "acl.conf",
		Attrdescription: "Network Lists",
		NetworkLists:    &NetworkLists{XMLName: xml.Name{}, List: listNode},
	}}
}

func XMLGatewayList(cserverList []*cserver.SipsCserver, list []*gateway.SipsGateway) []*Domain {
	var listGateway []*Gateway = make([]*Gateway, 0)

	for _, val := range list {
		var temp Gateway = Gateway{XMLName: xml.Name{}}
		temp.Attrname = val.Names
		var register = "false"
		if val.Register == 2 {
			register = "true"
		}

		temp.Param = []*Param{
			&Param{XMLName: xml.Name{}, Attrname: "realm", Attrvalue: val.Realm},
			&Param{XMLName: xml.Name{}, Attrname: "username", Attrvalue: val.Username},
			&Param{XMLName: xml.Name{}, Attrname: "password", Attrvalue: val.Password},
			&Param{XMLName: xml.Name{}, Attrname: "register", Attrvalue: register},
			&Param{XMLName: xml.Name{}, Attrname: "proxy", Attrvalue: val.Proxy},
		}

		listGateway = append(listGateway, &temp)
	}

	var domainArr []*Domain = make([]*Domain, 0)

	for _, vals := range cserverList {
		var tempDomain = Domain{
			XMLName:  xml.Name{},
			Attrname: vals.Domain,
			Params: &Params{
				XMLName: xml.Name{},
			},
			Variables: &Variables{
				XMLName: xml.Name{},
			},
		}

		domainArr = append(domainArr, &tempDomain)
	}

	return domainArr
}
